

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.hibernate.Session;


/**
 * Servlet implementation class EditHostFamily
 */
@WebServlet("/EditPersonalTrip")
public class EditPersonalTrip extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private int guestUserID;
	private Timestamp arrivalDate;
	private Timestamp departureDate;
	private int districtID;
	private int numberOfGuests;
	private double maxPrice;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public EditPersonalTrip() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession guestsession = request.getSession(true);
		guestsession.getAttribute("guestUserID");
		guestUserID = Integer.parseInt(guestsession.getAttribute("guestUserID").toString());
		System.out.println("guestUserID: " + guestUserID);
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		Date arrivalDay = new Date();
		try {
			arrivalDay = dateFormat.parse(request.getParameter("arrivalDate"));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Date departureDay = new Date();
		try {
			departureDay = dateFormat.parse(request.getParameter("departureDate"));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		arrivalDate = new Timestamp(arrivalDay.getTime());
		departureDate = new Timestamp(departureDay.getTime());
		districtID = Integer.parseInt(request.getParameter("district"));
		numberOfGuests = Integer.parseInt(request.getParameter("numberOfGuests"));
		maxPrice = Double.parseDouble(request.getParameter("maxPrice"));
		
		int guestTripID = Integer.parseInt(guestsession.getAttribute("guestTripID").toString());
		System.out.println(guestTripID);
		guestTrip guestTripObj = new guestTrip();
		if(guestTripID>0){
			try{
				Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		        session.beginTransaction();
		        guestTripObj = (guestTrip) session.get(guestTrip.class,guestTripID);
		        session.getTransaction().commit();
		        //request.getRequestDispatcher("guest-Trip.html").forward(request, response);
		        response.sendRedirect("personal-trip.html");
			} catch(Exception ex){
				HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().rollback();
			}
		}
		guestTripObj.setGuestUserID(guestUserID);
		guestTripObj.setDistrictID(districtID);
		guestTripObj.setArrivalDate(arrivalDate);
		guestTripObj.setDepartureDate(departureDate);
		guestTripObj.setNumberOfGuests(numberOfGuests);
		guestTripObj.setMaxPrice(maxPrice);
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
	        session.beginTransaction();
	        session.saveOrUpdate(guestTripObj);
	        session.getTransaction().commit();
		} catch(Exception ex){
			HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().rollback();
		}
	}

}
